<template>
  <div>
    <div class="adminhead">
      <img class="img-head" src="/public/adminIco.png" alt="xjtu" />
    </div>
    <div class="adminbody">
      <div class="login-box">
        <a-tabs v-model:activeKey="activeKey">
          <a-tab-pane key="1" tab="密码登录">
            <a-form :model="user" :wrapper-col="wrapperCol">
              <h1 class="login-box-title">后台登录</h1>
              <a-form-item>
                <a-input
                  v-model:value="user.u_key"
                  placeholder="请输入id/邮箱/手机号"
                >
                  <template #prefix><user-outlined type="user" /></template>
                </a-input>
              </a-form-item>

              <a-form-item>
                <a-input-password
                  v-model:value="user.password"
                  placeholder="请输入密码"
                >
                  <template #prefix><lock-outlined /></template>
                </a-input-password>
              </a-form-item>

              <a-form-item :wrapper-col="wrapperCol">
                <a-button type="primary" block @click="login">登录</a-button>
              </a-form-item>
            </a-form>
          </a-tab-pane>
          <a-tab-pane key="2" tab="短信登录">
            <a-form :model="tel" :wrapper-col="wrapperCol">
              <h1 class="login-box-title">后台登录</h1>
              <a-form-item>
                <a-input v-model:value="tel.u_key" placeholder="输入手机号">
                  <template #prefix><mobile-outlined /></template>
                </a-input>
              </a-form-item>

              <a-form-item>
                <a-input-search
                  v-model:value="tel.code"
                  placeholder="验证码"
                  @search="getTelVerificationCode()"
                >
                  <template #prefix><lock-outlined /></template>
                  <template #enterButton>
                    <a-button type="primary" v-if="tel_code" disabled
                      >已获得验证码</a-button
                    >
                    <a-button type="primary" v-else>获得验证码</a-button>
                  </template>
                </a-input-search>
              </a-form-item>

              <a-form-item :wrapper-col="wrapperCol">
                <a-button type="primary" block @click="login_tel"
                  >登录</a-button
                >
              </a-form-item>
            </a-form>
          </a-tab-pane>
          <a-tab-pane key="3" tab="邮箱登录">
            <a-form :model="mail" :wrapper-col="wrapperCol">
              <h1 class="login-box-title">后台登录</h1>
              <a-form-item>
                <a-input v-model:value="mail.u_key" placeholder="输入邮箱">
                  <template #prefix><mail-outlined /></template>
                </a-input>
              </a-form-item>

              <a-form-item>
                <a-input-search
                  v-model:value="mail.code"
                  placeholder="验证码"
                  @search="getMailVerificationCode()"
                >
                  <template #prefix><lock-outlined /></template>
                  <template #enterButton>
                    <a-button type="primary" v-if="mail_code" disabled
                      >已获得验证码</a-button
                    >
                    <a-button type="primary" v-else>获得验证码</a-button>
                  </template>
                </a-input-search>
              </a-form-item>

              <a-form-item :wrapper-col="wrapperCol">
                <a-button type="primary" block @click="login_mail"
                  >登录</a-button
                >
              </a-form-item>
            </a-form>
          </a-tab-pane>
          <a-tab-pane key="4" tab="申请管理员">
            <a-form :model="user" :wrapper-col="wrapperCol">
              <h1 class="login-box-title">申请成为管理员</h1>
              <a-form-item>
                <a-input
                  v-model:value="user.u_key"
                  placeholder="请输入id/邮箱/手机号"
                >
                  <template #prefix><user-outlined type="user" /></template>
                </a-input>
              </a-form-item>

              <a-form-item>
                <a-input-password
                  v-model:value="user.password"
                  placeholder="请输入密码"
                >
                  <template #prefix><lock-outlined /></template>
                </a-input-password>
              </a-form-item>

              <a-form-item :wrapper-col="wrapperCol">
                <a-button type="primary" block @click="Apply">申请</a-button>
              </a-form-item>
            </a-form>
          </a-tab-pane>
        </a-tabs>
       
        <a-modal
          v-model:visible="visible"
          width="500px"
          title="Basic Modal"
          @ok="handleOk"
        >
          <p>Some contents...</p>
          <p>Some contents...</p>
          <p>Some contents...</p>
        </a-modal>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, reactive } from "vue";
import { get, post, setLocalBToken, tip } from "@/common";
import { useRouter } from "vue-router";
import {
  UserOutlined,
  LockOutlined,
  MailOutlined,
  MobileOutlined,
} from "@ant-design/icons-vue";

const router = useRouter(); //获取路由器
const activeKey = ref("1");
const tel_code = ref(false);
const mail_code = ref(false);

const user = reactive({
  u_key: "",
  password: "",
});

const tel = reactive({
  u_key: "",
  code: "",
});

const mail = reactive({
  u_key: "",
  code: "",
});

const login = () => {
  post("/buser/login/passwordLogin", user).then((res) => {
    tip.success("登录成功！");
    const token = res.data;
    setLocalBToken(token);
    router.push({ path: "/buser/home" });
  });
};

const getTelVerificationCode = () => {
  console.log(tel.u_key);
  get("/buser/login/telcode?tel=" + tel.u_key).then((res) => {
    tel_code.value = true;
  });
};

const getMailVerificationCode = () => {
  console.log(mail.u_key);
  get("/buser/login/mailcode?mail=" + mail.u_key).then((res) => {
    mail_code.value = true;
  });
};

const login_tel = () => {
  post("/buser/login/telcodeLogin", tel).then((res) => {
    tip.success("登录成功！");
    const token = res.data;
    setLocalBToken(token);
    router.push({ path: "/buser/home" });
  });
};

const login_mail = () => {
  post("/buser/login/mailcodeLogin", mail).then((res) => {
    tip.success("登录成功！");
    const token = res.data;
    setLocalBToken(token);
    router.push({ path: "/buser/home" });
  });
};

const Apply = () =>{
  post("/buser/login/apply",user).then((res)=>{
    tip.success("已经发送请求，等待超级管理员批准");
  })
}

//一行24列
const wrapperCol = {
  offset: 5,
  span: 14,
};

const visible = ref(false);

const showModal = () => {
  visible.value = true;
};

const handleOk = (e) => {
  console.log(e);
  visible.value = false;
};
</script>

<style>
.login-box {
  width: 40%;
  min-width: 500px;
  max-width: 500px;
  box-shadow: 0px 0px 10px 10px rgba(0, 0, 0, 0.2);
  border-radius: 5px;
  padding: 20px;
  height: 430px;
  background-color: #00000068;
}
.login-box-title {
  text-align: center;
  font-family: 楷体;
  font-size: 36px;
  font-weight: bold;
  color: #ffffff;
  margin: 20px auto;
}
.adminhead {
  width: 100%;
  height: 2cm;
}
.img-head {
  margin-left: 1.5cm;
  height: 100%;
}

.adminbody {
  position: flex;
  top: 2cm;
  bottom: 0px;
  left: 0px;
  right: 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 16cm;
  background-image: url("/public/adminlogin.jpg");
  background-size: 1600px, 750px;
  background-repeat: no-repeat;
  background-position: center;
}

.adminfooter {
  text-align: center;
  margin-top: 3px;
}
</style>